<?php
if (!session_id()) {
    session_start();
}
$user_id = $_SESSION['user_id'];

global $wpdb;
$tb_img  = 'ap_painting_exam';
$tb_user = 'ap_painting_user';

if (isset($_GET['detail']) && $_GET['detail']) {
    $offset = 10;

    $userid = $_GET['detail'];

    $total = $wpdb->get_results("SELECT * FROM $tb_img WHERE user_id ={$userid}");
    $album = $wpdb->get_results("SELECT *, $tb_img.id as img_id FROM $tb_img LEFT JOIN $tb_user ON $tb_img.user_id = $tb_user.user_id WHERE $tb_img.user_id ={$userid} ORDER BY dateadd DESC");
    $flag = false;
    ?>
    <script type="text/javascript">
        function switchimg($elid) {
            jQuery('.gallery li.imgele').hide();
            jQuery('#' + $elid).fadeIn();
        }
        function changepage($pageid) {
            jQuery('.xpagina ul').hide();
            jQuery('.page_' + $pageid).show();
        }
       function loadgalery($pageid) {
            $.blockUI({css: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}});
            jQuery.post('<?php echo site_url() ?>/exam/?pagex=' + $pageid, function($data) {
                jQuery('#albumfuck').html($data);
                $.unblockUI();
            });
        }
    </script>
    <div class="left_col album">


        <ul class="gallery">


            <?php
            foreach ($album as $k => $row) {

                list($d, $m, $y) = explode("-", $row->birthday);
                $old = 2013 - $y;
                ?>
                <li id="ele_<?php echo $k ?>" class="imgele <?php echo $k ? ' hide' : '' ?>" >

                    <img class="<?php echo $flag ? '' : 'thumb'; ?>" src="<?php echo $row->url ?>?t=<?php echo time(); ?>">

                    <div class="author">
                        <div class="name"><?php echo $row->name ?>&nbsp; <span>(<?php echo $old ?> tuổi)</span></div>
                        <div class="xpagina">
                            <?php
                            $numpage = ceil(count($total) / $offset);
                            for ($j = 1; $j <= $numpage; $j++) {
                                ?>
                                <ul class="page_<?php echo $j ?> <?php echo $j == 1 ? '' : 'hide' ?>">
                                    <?php if ($numpage > 1 && $j != 1) { ?>
                                        <li class="vpagination"  onclick="changepage('<?php echo $j - 1 ?>');"> &laquo;  </li>
                                    <?php } ?>

                                    <?php for ($i = ($j - 1 ) * $offset; $i < $j * $offset && $i < count($total); $i++) { ?>
                                        <li class="<?php echo $i == $k ? 'active' : '' ?>" onclick="switchimg('ele_<?php echo $i ?>');"><a href="javascript:void(0)"><?php echo $i + 1 ?></a></li>
                                    <?php } ?>
                                    <?php if ($numpage > 1 && $j != $numpage) { ?>
                                        <li class="vpagination" onclick="changepage('<?php echo $j + 1 ?>');"> &raquo; </li>
                                    <?php } ?>
                                </ul>
                            <?php } ?>
                        </div>
                    </div>
                    <div style="clear: both"></div>
                    <img style="box-shadow: 0 0 0; height: 20px; margin-top: -10px; width: 100%;" src="<?php echo bloginfo('template_url'); ?>/img/shadow.png" />

                </li>

            <?php } ?>

        </ul>
    </div>


    <div class="pagination right_col">
        <ul> 
            <?php
            $allpage = $wpdb->get_results("SELECT * FROM $tb_img LEFT JOIN $tb_user ON $tb_img.user_id = $tb_user.user_id GROUP BY $tb_img.user_id");
           $totalpage = ceil(count($allpage) / 9); 
            for ($pagex = 1; $pagex <= $totalpage; $pagex++) {
                $i = $totalpage - $pagex;
                ?>
                <li><a href="javascript:void(0) " onclick="loadgalery('<?php echo $i ?>')"><?php echo $i+1 ?></a></li>
            <?php } ?>
        </ul>
		<div id="backbt" onclick="loadgalery('<?php echo $i ?>')">&laquo; Back</div>
    </div>


<?php } else { ?>
    <div class="left_col album">
        <?php
        global $wpdb;
        $user_id = $_SESSION['user_id'];
        $tb_img = 'ap_painting_exam';
        $tb_user = 'ap_painting_user';
        $total = $wpdb->get_results("SELECT * FROM $tb_img LEFT JOIN $tb_user ON $tb_img.user_id = $tb_user.user_id GROUP BY $tb_img.user_id");
        $currpage = isset($_GET['pagex']) ?  $_GET['pagex'] : 0 ; 
        for ($pagex = 0; $pagex <= ceil(count($total) / 9); $pagex++) {
            $offset = $pagex ? 9 : 8;
            $from = $pagex * $offset;
            $limit = " LIMIT $from,$offset ";

            $owneralbum = $wpdb->get_row("SELECT * FROM $tb_img LEFT JOIN $tb_user ON $tb_img.user_id = $tb_user.user_id WHERE  $tb_user.user_id = {$user_id} ");
            $album = $wpdb->get_results("SELECT * FROM $tb_img LEFT JOIN $tb_user ON $tb_img.user_id = $tb_user.user_id  WHERE  $tb_user.user_id != {$user_id} GROUP BY $tb_img.user_id  $limit");
            $flag = true;
            ;
            ?>

            <ul id="exam_<?php echo $pagex ?>" class="gallery <?php echo (!$pagex  && !$currpage) || $currpage ? '' : 'hide' ?>" >
                <?php
                $row = $owneralbum;
                $thumimg = $wpdb->get_row("SELECT * FROM $tb_img WHERE user_id = $row->user_id ORDER BY dateadd DESC LIMIT 1");
                ?>
                <li>
                    <span><img src="<?php echo bloginfo('template_url'); ?>/img/bangkeo.png"></span>


                    <img src="<?php echo $thumimg->url ?>?t=<?php echo time(); ?>">
                    <a href="javascript:void(0)" onclick="loadfimg(<?php echo $row->user_id ?>)">
                        <div class="info">
                            Bài thi của bé <br /> <span><?php echo $row->name ?></span> <br /> (<?php echo $old ?> tuổi)
                        </div>
                    </a>

                </li>
                <?php
                foreach ($album as $k => $row) {
                    $thumimg = $wpdb->get_row("SELECT * FROM $tb_img WHERE user_id = $row->user_id ORDER BY dateadd DESC LIMIT 1");
                    list($d, $m, $y) = explode("-", $row->birthday);
                    $old = 2013 - $y;
                    ?>
                    <li id="ele_<?php echo $k ?>" class="imgele <?php echo $k ? ' hide' : '' ?>" >
                        <span><img src="<?php echo bloginfo('template_url'); ?>/img/bangkeo.png"></span>
                        <img src="<?php echo $thumimg->url ?>?t=<?php echo time(); ?>">
                        <a href="javascript:void(0)" onclick="loadfimg(<?php echo $row->user_id ?>)">
                            <div class="info">
                                Bài thi của bé <br /> <span><?php echo $row->name ?></span> <br /> (<?php echo $old ?> tuổi)
                            </div>
                        </a>

                    </li>

                <?php } ?>

            </ul>
        <?php } ?>
    </div>
    <?php if (count($total) > $offset) { ?>
        <div class="pagination right_col">
            <ul> 
                <?php
                $totalpage = ceil(count($total) / $offset);
                for ($j = 0; $j < $totalpage; $j++) {
                    $i = $totalpage - $j;
                    ?>
                    <li><a href="javascript:void(0);" onclick="switchgalery(<?php echo $i - 1 ?>);"><?php echo $i ?></a></li>
                <?php } ?>

            </ul>
        </div>
    <?php } ?>
<?php } ?>
